<?php
		session_start();
define('TEMPLATES', '../templates/');
define('JS','../js/');
define('CSS','../css/');
define('OBJETOS','../objetos/');
define('NEGOCIOS','../negocio/');
define('EXTENSION','.phtml');
function __autoload($class_name){
  require_once $class_name . '.php';
}
 
Class TransaccionArticulo {
	
	var $AdminObj;
	var $generador_xhtml;
	var $vars;
	
	function TransaccionArticulo($array_vars){
		$this->AdminObj=new AdminArticulo();
		$this->vars=$array_vars;
		$this->generador_xhtml=new GeneradorXHTML();
	}

	function transNewArticulo(){
		$Articulo=$this->AdminObj->getNewObject("Articulo",array(Articulo));

		
		$this->generador_xhtml->mostrarTpl($Articulo,array("tpl"=>"Articulo"  ,"boton"=>"Save","accion"=>"transSetArticulo","css"=>"Articulo","js"=>"Articulo"));
	}
	
	function transSetArticulo(){
		$this->vars['id_articulo']=$this->AdminObj->getNewID("Articulo","id_Articulo");
		$Articulo=$this->AdminObj->getNewObject("Articulo",$this->vars);
		$this->AdminObj->setArticulo($Articulo);
		$this->AdminObj->redireccionar("TransaccionArticulo.php");
	}
	
	function transGetArticulo(){
		$Articulo=$this->AdminObj->getArticulo($this->vars['id_articulo']);
		
		
		
		$array_elementos=array("tpl"=>"Articulo"  ,"boton"=>"Guardar cambios","accion"=>"transUpdateArticulo","css"=>"Articulo","js"=>"Articulo");
		$this->generador_xhtml->mostrarTpl($Articulo,$array_elementos);
	}
	
	function transUpdateArticulo(){
		$Articulo=$this->AdminObj->getNewObject("Articulo",$this->vars);
		$this->AdminObj->updateArticulo($Articulo);
		$this->AdminObj->redireccionar("TransaccionArticulo.php");	
	}
	
	function transDeleteArticulo(){
		$this->AdminObj->deleteArticulo($this->AdminObj->getArticulo($this->vars['id_articulo']));
		//$this->AdminObj->redireccionar("TransaccionArticulo.php");
	}
	function transGetArticulos()
	{
		$rows=$this->generador_xhtml->generarRowsTable($this->AdminObj->getArticulos(),"checkbox",false,0);
		$this->generador_xhtml->mostrarTpl("",array("tpl"=>"Articulostable","rows"=>$rows,"js"=>"Articulostable.js"));
	}
    
    function transMostrarConversion(){
        $this->generador_xhtml->mostrarTpl("",array("tpl"=>"formConversion","accion"=>"transConvertir","boton"=>"convertir"));
    }
    
    function transConvertir(){
        $bolivianos=$this->vars['bolivianos'];
        $tipo=$this->vars['cambio'];
        $bolivianos_a_dolares=$this->AdminObj->convertir($this->vars['bolivianos'],$tipo);
        $this->generador_xhtml->mostrarTpl("",array("tpl"=>"conversion","conversion"=>$bolivianos_a_dolares));
    }		
}

if($_GET){
	$vars_trans=array_slice($_GET,1);
//antes no olvidar poner una validador para que nadie pueda ejecutar las funciones
	call_user_func(array(new TransaccionArticulo($vars_trans),current($_GET)?current($_GET):"transGetArticulos"));
}
else if($_POST){
		$vars_trans=array_slice($_POST,1);
		call_user_func(array(new TransaccionArticulo($vars_trans),current($_POST)?current($_POST):"transGetArticulos"));
		}
	else	call_user_func(array(new TransaccionArticulo(array()),"transGetArticulos"));

?>